package Controller;

import Model.Account;
import Model.AccountDB;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by FreyrWings on 2017/6/4.
 * Deal with the requests of the homepage
 */
public class Blog extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        if(req.getSession().getAttribute("token")==null){// check if the token exists
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            String type = req.getParameter("type");
            if(type==null){
                resp.sendRedirect("login");// directly visiting the homepage will be redirected to the login page
                return;
            }

            if(type.equals("login")){
                String token="";
                if(req.getSession().getAttribute("token")==null){
                    try{
                        Integer aid = AccountDB.identify(username, password);
                        if(aid>0){
                            String tok = SHA.getResult(username+"salt"+password+ System.currentTimeMillis());
                            req.getSession().setAttribute("token", tok);
                            AccountDB.updateToken(aid, tok);// update token each time the user login with username and password
                            req.getRequestDispatcher("/home").forward(req,resp);
                        }
                        else{
                            resp.sendRedirect("login");
                        }
                    }
                    catch (Exception e){
                        e.printStackTrace();
                        resp.sendRedirect("login");
                    }
                }
                else{
                    token += req.getSession().getAttribute("token");
                    if(AccountDB.identify(token)==0){
                        resp.sendRedirect("login");
                    }
                    else{
                        req.getRequestDispatcher("/home").forward(req,resp);
                    }
                }
            }
            else if(type.equals("regis")){
                if(AccountDB.usernameUnique(username)){
                    String token = SHA.getResult(username+"salt"+password);
                    req.getSession().setAttribute("token", token);
                    Account account = new Account();
                    account.setUsername(username);
                    account.setPassword(password);
                    account.setToken(token);
                    AccountDB.create(account);
                    resp.sendRedirect("/");
                }
                else{
                    System.out.println("already exist");
                    resp.sendRedirect("login");
                }
            }

            resp.setStatus(400);
        }
        else{
            String token = req.getSession().getAttribute("token").toString();
            System.out.println(token);
            int id = AccountDB.identify(token);
            if(id > 0){
                req.getRequestDispatcher("index.html").forward(req,resp);
            }
            else{
                resp.setStatus(400);
            }
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().removeAttribute("token");// remove the token from session
    }
}
